//
//  UserAdressVC.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/27.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserAdressVC.h"
#import "UserGroup.h"
#import "UserItem.h"
#import "UserSaveItem.h"
#import "UserArrowItem.h"
#import "UserCenterCell.h"
#import "UserSeriesViewModel.h"
#import "UserPersonalDetailVC.h"
#import "UserSericesChildVC.h"

@interface UserAdressVC ()

@property (nonatomic, strong) UserCenterCell *currentCell;
@end

@implementation UserAdressVC {
    
    BOOL _isChinese;
    UserSeriesViewModel *_seriesViewModel;
    User *_user;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _user = [User getLastUser];
    _seriesViewModel = [UserSeriesViewModel shareInstance];
    _isChinese = [NSBundle currentLanguageIsChinese];

    if (_isChinese) {
        [self loadChineseData];
    }else {
       [self loadEnglishNationData];
    }
    
}


- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (!_isChinese) {
        [[NSNotificationCenter defaultCenter] postNotificationName:userChangeAdressNotification object:self.currentCell.item.title];
    }
}


- (void)loadEnglishNationData {
    UserGroup *group0 = [[UserGroup alloc] init];
    group0.headHeight = kAutoHei(24);
    
    for (NSString *nationName in _seriesViewModel.nations) {
        
        UserSaveItem *item = [UserSaveItem itemWithTitle:nationName];
        [group0.items addObject:item];
        
    }
    [self.groups addObject:group0];
}

- (void)loadChineseData {
    UserGroup *group0 = [[UserGroup alloc] init];
    group0.headHeight = kAutoHei(24);
    
    __weak typeof(self) weakSelf = self;
    
    for (NSString *provinces in _seriesViewModel.provinces) {
        UserArrowItem *item = [UserArrowItem itemWithTitle:provinces andTarget:nil];
        
        __weak typeof(item) weakItem = item;
        
        [item setBlock:^{
            UserSericesChildVC *childVc = [[UserSericesChildVC alloc] init];
            childVc.childType = SericesChildCity;
            _seriesViewModel.selectProvices = weakItem.title;
            
            [weakSelf.navigationController pushViewController:childVc animated:YES];
        }];
        
        [group0.items addObject:item];
    }
    [self.groups addObject:group0];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserCenterCell *cell = [UserCenterCell settingViewCellTableView:tableView withStyle:UITableViewCellStyleValue1];
    
    UserGroup *group = self.groups[indexPath.section];
    UserSaveItem *item = group.items[indexPath.row];
    cell.item = item;
    
    if (_isChinese) {
        
    }else {
        if (self.currentCell == nil && [item.title isEqualToString:_user.userInforItem.location]) {
            self.currentCell = cell;
            cell.selected = YES;
            [cell setSelected:YES animated:NO];
            
        }
    }
    
    if (indexPath.row == group.items.count - 1) {
        [cell showLine:NO];
    }else {
        [cell showLine:YES];
    }
    if (indexPath.row == 0) {
        [cell showSectionSepTopLineView:YES];
    }else {
        [cell showSectionSepTopLineView:NO];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取出对应的模型数据
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    
    // 2.判断有没有block,有的话就执行
    if (item.block) {
        item.block();
        return;
    }
    if (!_isChinese) {
        UserCenterCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        
        if (self.currentCell) {
            self.currentCell.selected = NO;
        }
        
        cell.selected = YES;
        self.currentCell = cell;
    }
    
    
}

@end


